<template>
  <div id="admin" class="container">
    <el-table
      :data="tableData"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
      border
      style="width: 100%"
    >
      <el-table-column prop="id" label="ID" width="120px" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="user_id" label="会员ID" />
      <el-table-column prop="ip" label="登录IP" />
      <el-table-column prop="login_address" label="登录地址" />
      <el-table-column prop="agent" label="客户端" />
      <el-table-column prop="desc" label="描述" />
      <el-table-column prop="create_time" label="添加时间" />
      <el-table-column label="操作" width="200px" fixed="right">
        <template slot-scope="scope">
          <el-button class="dele" size="mini" @click="delUserLog(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      :hide-on-single-page="page.total < page.size ? true : false"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>
import userApi from '@/api/users.js'
export default {
  data() {
    return {
      activeName: 'first',
      tableData: [],
      searchForm: {},
      dialogAdmin: false,
      options: [], // 角色
      name: '', // 暂存会员组姓名
      adminForm: {
        title: '',
        desc: '',
        id: ''
      }, // 新增会员组表单
      adminRules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }]
      },
      page: {
        total: 0,
        currentPage: 1,
        size: 10,
        pageSizes: [10, 20, 30, 40]
      }, // 分页器
      val: {}
    }
  },
  created() {
    this.getList()
    // this.getwebList()
  },
  updated() {},
  methods: {
    open() {
      this.dialogAdmin = true
      this.adminForm = {
        title: '',
        desc: '',
        id: ''
      }
    },
    editUserGroup(row) {
      this.dialogAdmin = true
      this.adminForm = row
    },
    delUserLog(row) {
      userApi.delUserLoginLog({ id: row.id }).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.getList()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 获取列表
    getList() {
      this.val.page = this.page.currentPage
      this.val.limit = this.page.size
      this.val.account = this.searchForm.account
      userApi.userLoginLog(this.val).then(res => {
        this.tableData = res.data
        this.page.total = res.count
      })
    },

    // 提交
    submit() {
      this.$refs.adminForm.validate(valid => {
        if (valid) {
          if (!this.adminForm.id) {
            userApi
              .userGroupAdd({
                ...this.adminForm
              })
              .then(res => {
                if (res.code === 0) {
                  this.$message.success(res.msg)
                  this.dialogAdmin = false
                  this.getList()
                } else {
                  this.$message.error(res.msg)
                }
              })
          } else {
            userApi.userGroupEdit({ ...this.adminForm }).then(res => {
              if (res.code === 0) {
                this.$message.success(res.msg)
                this.dialogAdmin = false
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        } else {
          this.$message.warning('请填写必要信息')
        }
      })
    },

    // 搜索
    search() {
      this.$refs.searchForm.validate(() => {
        this.page.currentPage = 1
        this.getList()
      })
    },

    // 重置搜索框
    reset() {
      this.searchForm = {
        account: ''
      }
    },
    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getList()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>
#admin {
  .el-form-item {
    width: 100%;
  }
  .el-form-item__content {
    width: calc(100% - 80px);
    .el-select {
      width: calc(100%);
    }
  }
}
.tabs {
  padding: 15px 10px;
}

#admin {
  background-color: #fff;
}

.el-table {
  margin-bottom: 20px;
}
.search {
  background-color: #fff;
  padding: 15px 10px;
  margin-bottom: 10px;
  .el-form-item {
    margin-bottom: 0;
  }
  .el-button {
    font-size: 14px;
  }
}
.btn {
  text-align: center;
}
</style>
